﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using DevComponents.DotNetBar.Controls;
using System.IO;
using System.Drawing;
using System.Data;

namespace PlanningGIS.Win.Controls
{
    /// <summary>
    /// 控件操作类
    /// </summary>
    public class ControlHelper
    {


        /// <summary>
        /// 用组合框中所有的项填充DataGridViewX
        /// </summary>
        /// <param name="dgvX"></param>
        /// <param name="dgvcbC"></param>
        public static void FillAllCode(DataGridViewX dgvX, DataGridViewComboBoxColumn dgvcbC)
        {
            dgvX.Rows.Clear();
            for (int i = 0; i < dgvcbC.Items.Count; i++)
            {
                dgvX.Rows.Add();
                dgvX.Rows[dgvX.RowCount].Cells[dgvcbC.Index].Value = dgvcbC.Items[i];
            }
        }

        /// <summary>
        /// 设置CheckedListBox全选或全不选
        /// </summary>
        /// <param name="chklstbox"></param>
        /// <param name="state"></param>
        public static void ChangeChklstboxState(CheckedListBox chklstbox, bool state)
        {
            if (chklstbox.Items.Count == 0) return;
            for (int i = 0; i < chklstbox.Items.Count; i++)
            {
                chklstbox.SetItemChecked(i, state);
            }
        }

        /// <summary>
        /// 对于DataGridView控件上的复选框列，单击其列头，设置全选或者全部取消
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        public static void dataGridViewX_ColumnHeaderMouseClick(object sender, DataGridViewCellMouseEventArgs e)
        {
            DataGridViewX dgvX = (DataGridViewX)sender;
            if (dgvX.Columns[e.ColumnIndex] is DataGridViewCheckBoxColumn)
            {
                bool hasCheck = false;

                foreach (DataGridViewRow dgvR in dgvX.Rows)
                {
                    if (Convert.ToBoolean(dgvR.Cells[e.ColumnIndex].Value))
                    {
                        hasCheck = true;
                        break;
                    }

                }

                foreach (DataGridViewRow dgvR in dgvX.Rows)
                {
                    dgvR.Cells[e.ColumnIndex].Value = (!hasCheck);
                }
            }
        }

        /// <summary>
        /// DataGridViewX错误处理
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        public static void dataGridViewX_DataError(object sender, DataGridViewDataErrorEventArgs e)
        {
            Console.WriteLine(e.Exception.Message);
        }

        /// <summary>
        /// 将字节流读到图片框中
        /// </summary>
        /// <param name="pb"></param>
        /// <param name="bImg"></param>
        public static void ShowImgFromBlog(PictureBox pb, byte[] bImg)
        {
            try
            {
                if (bImg == null || bImg.Length == 0)
                {
                    pb.Image = null;
                    return;
                }
                MemoryStream mStream = new MemoryStream(bImg);
                mStream.Position = 0;
                Image img = Image.FromStream(mStream);
                mStream.Close();
                pb.Image = img;
            }
            catch
            {
                pb.Image = null;
                return;
            }
        }

        /// <summary>
        /// 从PIC从读取图片
        /// </summary>
        /// <param name="pb"></param>
        /// <returns></returns>
        public static byte[] ReadImgFromPicbox(PictureBox pb)
        {
            Image img = pb.Image;
            if (img == null)
            {
                return null;
            }
            {
                Stream mStream = new MemoryStream();
                img.Save(mStream, img.RawFormat);

                byte[] bt = ((MemoryStream)mStream).GetBuffer();
                return bt;

                //Stream myStream = openFileDialog1.OpenFile();

                //int length = (int)myStream.Length;

                //byte[] bytes = new byte[length];

                //myStream.Read(bytes, 0, length);

                //myStream.Close();
            }
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="ParentCtl"></param>
        /// <param name="blnLock"></param>
        public static void ControlsLock(Control ParentCtl, bool blnLock)
        {
            foreach (Control ctl in ParentCtl.Controls)
            {
                if (ctl.Controls.Count > 0)
                {
                    ControlsLock(ctl, blnLock);
                }
                else
                {
                    if (ctl is TextBox || ctl is DevComponents.DotNetBar.Controls.TextBoxX)
                    {
                        ctl.Enabled = blnLock;
                    }
                    else if (ctl is ComboBox || ctl is DevComponents.DotNetBar.Controls.ComboBoxEx)
                    {
                        ctl.Enabled = blnLock;
                    }
                    else if (ctl is RichTextBox)
                    {
                        ctl.Enabled = blnLock;
                    }
                    else if (ctl is CheckBox || ctl is DevComponents.DotNetBar.Controls.CheckBoxX)
                    {
                        ctl.Enabled = blnLock;
                    }
                    else if (ctl is ListBox)
                    {
                        ctl.Enabled = blnLock;
                    }
                    else if (ctl is Button || ctl is DevComponents.DotNetBar.ButtonX)
                    {
                        ctl.Enabled = blnLock;
                    }
                    else if (ctl is DateTimePicker || ctl is DevComponents.Editors.DateTimeAdv.DateTimeInput)
                    {
                        ctl.Enabled = blnLock;
                    }
                    else if (ctl is RadioButton)
                    {
                        ctl.Enabled = blnLock;
                    }
                }
            }
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="sS"></param>
        /// <param name="LB"></param>
        public static void SplitStr2ListBox(string sS, ListBox LB)
        {
            LB.Items.Clear();
            if (sS == "")
            {
                return;
            }
            char[] cs = new Char[] { ';' };
            string[] sSplit;
            if (sS.Substring(sS.Length - 1, 1) == ";")
            {
                sS = sS.Substring(0, sS.Length - 1);
                sSplit = sS.Split(cs);
                for (int i = 0; i <= sSplit.Length - 1; i++)
                {
                    LB.Items.Add(sSplit[i]);
                }
            }
        }

        /// <summary>
        /// 清除控件中的Text属性值
        /// </summary>
        /// <param name="ParentCtl"></param>
        public static void ControlsClear(Control ParentCtl)
        {
            foreach (Control ctl in ParentCtl.Controls)
            {
                if (ctl.Controls.Count > 0)
                {
                    ControlsClear(ctl);
                }
                else
                {
                    ctl.Tag = null;
                    if (ctl is TextBox)
                    {
                        ctl.Text = "";
                    }
                    else if (ctl is ComboBox)
                    {
                        ctl.Text = "";
                    }
                    else if (ctl is RichTextBox)
                    {
                        ctl.Text = "";
                    }
                    else if (ctl is PictureBox)
                    {
                        PictureBox pb = (PictureBox)ctl;
                        pb.Image = null;
                    }
                    else if (ctl is CheckBox)
                    {
                        CheckBox cb = (CheckBox)ctl;
                        cb.Checked = false;
                    }
                }
            }
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="dt"></param>
        /// <param name="strFldName"></param>
        /// <param name="cbo"></param>
        public static void AddItem2cbo(DataTable dt, string strFldName, ComboBox cbo)
        {
            foreach (DataRow dr in dt.Rows)
            {
                cbo.Items.Add(dr[strFldName]);
            }
            if (cbo.Items.Count > 0)
                cbo.SelectedIndex = 0;
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private static void dataGridViewDataError(object sender, DataGridViewDataErrorEventArgs e)
        {
            System.Console.WriteLine(e.Exception.Message);
        }
    }
}
